<?php 
/* SVN FILE: $Id$ */
/* StoreController Test cases generated on: 2009-10-26 16:13:44 : 1256541224*/
App::import('Controller', 'Store');
App::import('Model','Order');
class StoreTestController extends StoreController {
	var $autoRender = false;
	function redirect($url=NULL,$code=NULL) { 
		$this->lastRedirectUrl = $url; 
		$action = $this->params['action'];
		#debug("$action action redirects to $url");
		$this->Session->write('test.lastRedirectUrl',$url);
		return true;
	}
}

class StoreControllerTest extends CakeTestCase {
	var $Store = null;
	# fixtureつかったり、テーブルからコピーしたりと挙動がよくわからん。fixtureつかうように統一するか？
	var $fixtures = array('app.order','app.product');
	function startTest() {
		$this->Store = new StoreTestController();
		$this->Store->constructClasses();
		
		$this->Order =& ClassRegistry::init('Order');
		$this->Order->deleteAll('Order.id > 0');
		$f = new OrderFixture;
		#debug($f->getRecords());
		foreach( $f->getRecords() as $key => $value ){
			$this->{$key} = $value;
		}
	}
	function testStoreControllerInstance() {
		$this->assertTrue(is_a($this->Store, 'StoreController'));
	}

	function testStoreIndex(){
		echo("<p style='font-size:12pt;margin:0.5em'><b>インデックスページの表示</b></p>");
		$result = $this->testAction('/depot/store/index',array('return'=>'vars'));
		#debug($result);
		$this->assertEqual(count($result['products']),2);
	}
	function testAddToCart(){
		echo("<p style='font-size:12pt;margin:0.5em'><b>カートへの追加</b></p>");
		$this->Store->Session->del('cart');
		$result = $this->testAction('/depot/store/add_to_cart/1',array('redirect' => 'false'));
		$this->assertEqual($this->Store->Session->read('test.lastRedirectUrl'),'/store/display_cart');
		$result = $this->testAction('/depot/store/display_cart',array('return'=>'vars'));
		$this->assertEqual(count($result['cart']->items()),1);
	}
	function testAddToCartInvalidProduct(){
		echo("<p style='font-size:12pt;margin:0.5em'><b>カートへ不正な商品の追加</b></p>");
		$result = $this->testAction('/depot/store/add_to_cart/123456',array('redirect' => 'false'));
		$this->assertEqual($this->Store->Session->read('test.lastRedirectUrl'),'/store/');
		$flash = $this->Store->Session->read('Message.flash');
		#debug($flash['message']);
		$this->assertEqual($flash['message'],'無効な商品です');
	}
	function testDisplayEmptyCart(){
		echo("<p style='font-size:12pt;margin:0.5em'><b>空のカートの表示</b></p>");
		$this->Store->Session->del('cart');
		$result = $this->testAction('/depot/store/display_cart',array('redirect'=>'false'));
		$this->assertEqual($this->Store->Session->read('test.lastRedirectUrl'),'/store/');
		#debug($result['cart']);
		$flash = $this->Store->Session->read('Message.flash');
		#debug($flash['message']);
		$this->assertEqual($flash['message'],'現在カートには商品が入っていません');
	}
	function testCheckOut(){
		echo("<p style='font-size:12pt;margin:0.5em'><b>チェックアウト</b></p>");
		$this->testAddToCart();
		$result = $this->testAction('/depot/store/checkout',array('return'=>'vars'));
		#debug($result['cart']);
		$this->assertTrue(!empty($result['cart']));
	}
	function testSaveInvalidOrder(){
		echo("<p style='font-size:12pt;margin:0.5em'><b>不正注文処理</b></p>");
		$this->testAddToCart();
		$data = array(
  		  	"Order" => array(
       			 "name" => "fred",
				 'email' => null,
				 'address' => null
    		),
		);
		$result = $this->testAction('/depot/store/save_order', array(
    		'data' => $data,
    		'fixturize' => false,
    		'method' => 'post',
    		'return' => 'vars')
		);
		$flash = $this->Store->Session->read('Message.flash');
		#debug($flash['message']);
		$this->assertEqual($flash['message'],'チェックアウトに失敗しました');
		#debug($result);
		$this->assertEqual(count($result['cart']->items()),1);
	}
	function testSaveValidOrder(){
		echo("<p style='font-size:12pt;margin:0.5em'><b>正常注文処理</b></p>");
		
		$this->testAddToCart();
		$this->assertEqual(count($this->Store->Session->read('cart')->items()),1);
		
		$this->Order->recursive = -1;
		$result = $this->Order->find('count');
		$this->assertEqual($this->Order->find('count'),0);
		
		$result = $this->testAction('/depot/store/save_order', array(
    		'data' => $this->valid_order_for_fred,
    		'fixturize' => false,
    		'method' => 'post',
    		'redirect' => 'false')
		);
		$this->assertEqual($this->Store->Session->read('test.lastRedirectUrl'),'/store/');
		$flash = $this->Store->Session->read('Message.flash');
		#debug($flash['message']);
		$this->assertEqual($flash['message'],'ご注文ありがとうございました');

		$cart = $this->Store->Session->read('cart');
		#debug($cart);
		$this->assertEqual(count($cart->items()),0);
		
		$result = $this->Order->find('count');
		$this->assertEqual($this->Order->find('count'),1);
	}
	function endTest() {
		unset($this->Store);
	}
	function tearDown(){
		unset($this->Store);
	}
}
?>